Imports TreeCreeper01
Imports TreeCreeper02
Imports TreeCreeper01Interfaces

Public Class fMain

    Private pCurrentStudy As IStudyNonSpatial
    Private pCurrentStudyName As String

    Private Sub DisplayActualStatistics()

        Dim TextLines() As String
        Dim BaseSpeciesList As ISpeciesAbundanceList
        Dim ListFilter As IListFilter
        Dim FilteredSpeciesList As ISpeciesAbundanceList
        Dim PlotIndex As Integer

        TextActual.Text = ""

        With pCurrentStudy.SampledPlots
            ReDim TextLines(.Count + 1)
        End With

        With pCurrentStudy
            BaseSpeciesList = .CreateBaseSpeciesAbundanceList(.Abundances)
            TextLines(0) = "All plots: " & CStr(BaseSpeciesList.SpeciesListAnalysis.CalculateGenericNonSpatialTaxonomicDiversity)
        End With

        With pCurrentStudy.SampledPlots
            For PlotIndex = 1 To .Count
                ListFilter = pCurrentStudy.SampledPlots.ListFilterByIndexBaseOne(PlotIndex)
                FilteredSpeciesList = BaseSpeciesList.NewSpeciesListByListFilter(ListFilter)
                TextLines(PlotIndex) = "Plot " & ListFilter.Name & ": " & FilteredSpeciesList.SpeciesListAnalysis.CalculateGenericNonSpatialTaxonomicDiversity
            Next
        End With

        TextActual.Lines = TextLines

    End Sub


    Private Sub PopulateStudy(ByVal Filename As String, Optional ByVal TaxonomicOverrideFilename As String = "")

        Dim StudyNonSpatial As IStudyNonSpatial
        Dim DataAgentNonSpatial As IDataAgentNonSpatial

        DataAgentNonSpatial = TreeCreeper02.CreateNonSpatialDataAgent(Filename, True, TaxonomicOverrideFilename)

        Me.Cursor = Cursors.WaitCursor

        StudyNonSpatial = New cStudyNonSpatial

        DataAgentNonSpatial.PopulateStudyNonSpatial(StudyNonSpatial)

        pCurrentStudyName = GetFileTitleFromFilename(Filename)
        pCurrentStudy = StudyNonSpatial


        If pCurrentStudy.Abundances.Count > 0 Then
            CheckDoUseAbundances.Checked = True
            CheckDoUseAbundances.Enabled = True
        Else
            CheckDoUseAbundances.Checked = False
            CheckDoUseAbundances.Enabled = False
        End If


        UpdateCaption()
        DisplayActualStatistics()

        NumberOfSpeciesSelector.Maximum = pCurrentStudy.TaxonomicTree.AllSpecies.Count
        NumberOfSpeciesSelector.Minimum = 1
        NumberOfSpeciesSelector.Value = NumberOfSpeciesSelector.Maximum
        LabelCountOfSpecies.Text = "of " & NumberOfSpeciesSelector.Maximum

        Me.ButtonShowTaxonomicTree.Enabled = True

        Me.Cursor = Cursors.Default

    End Sub


    Private Sub CheckOverride_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckOverride.CheckedChanged

        Me.ButtonSelectOverrideFile.Enabled = CheckOverride.Checked
        Me.TextOverrideFilename.Enabled = CheckOverride.Checked

    End Sub

    Private Sub ButtonSelectFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSelectFile.Click

        Dim MyOpenFileDialog As OpenFileDialog
        Dim Filename As String

        MyOpenFileDialog = New OpenFileDialog
        With MyOpenFileDialog
            .Title = "Select a file with non-spatial data for analysis"
            .DefaultExt = "csv"
            .CheckFileExists = True
            .Filter = "Non-spatial files (*.csv)|*.csv"
            .ShowDialog()
            Filename = .FileName
        End With

        If Not Filename = "" Then
            TextFilename.Text = Filename
            ButtonCreateStudy.Enabled = True
        End If

    End Sub

    Private Sub UpdateCaption()

        Dim TheCaption As String

        TheCaption = " TreeCreeper NonSpatial "

        If pCurrentStudyName <> "" Then
            TheCaption = TheCaption & "- " & pCurrentStudyName
        End If

        Me.Text = TheCaption

    End Sub

    Public Sub New()

        InitializeComponent()

    End Sub

    Private Sub ButtonShowTaxonomicTree_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonShowTaxonomicTree.Click

        Dim NewTreeDisplay As fSpeciesTree

        NewTreeDisplay = New fSpeciesTree
        NewTreeDisplay.DisplayTaxonomicTree(pCurrentStudy, "Taxonomic tree from: " & pCurrentStudyName)

    End Sub

    Private Sub fMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        UpdateCaption()

    End Sub

    Private Function GetFileTitleFromFilename(ByVal Filename As String) As String

        Dim Index As Integer

        Index = InStrRev(Filename, "\")
        If Index > 0 Then
            Index = Len(Filename) - Index
            Return Microsoft.VisualBasic.Right(Filename, Index)
        Else
            Return Filename
        End If

    End Function

    Private Sub ButtonDoRandom_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonDoRandom.Click

        Dim RandomRun As fRandomisedRun
        Dim Caption As String

        Caption = "Random for " & pCurrentStudyName & ", " & TextHowManyRuns.Text & " runs, " & NumberOfSpeciesSelector.Value & " species"

        RandomRun = New fRandomisedRun
        RandomRun.DoABatchOfRuns(pCurrentStudy, CInt(TextHowManyRuns.Text), CInt(Me.NumberOfSpeciesSelector.Value), _
            Me.CheckDoUseAbundances.Checked = True, Caption)

    End Sub

    Private Sub ButtonSelectOverrideFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSelectOverrideFile.Click

        Dim MyOpenFileDialog As OpenFileDialog
        Dim Filename As String

        MyOpenFileDialog = New OpenFileDialog
        With MyOpenFileDialog
            .Title = "Select an text file with the override data"
            .DefaultExt = "txt"
            .CheckFileExists = True
            .Filter = "Text files (*.txt)|*.txt"
            .ShowDialog()
            Filename = .FileName
        End With

        If Not Filename = "" Then
            TextOverrideFilename.Text = Filename
        End If

    End Sub

    Private Sub ButtonCreateStudy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCreateStudy.Click

        Dim Filename As String
        Dim OverrideFilename As String

        Filename = Me.TextFilename.Text
        If Me.CheckOverride.Checked Then
            OverrideFilename = Me.TextOverrideFilename.Text
        Else
            OverrideFilename = ""
        End If

        PopulateStudy(Filename, OverrideFilename)


    End Sub

End Class